package com.jyw.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jyw.App;
import com.jyw.R;
import com.jyw.network.Server;
import com.jyw.pojo.AllClass;
import com.jyw.utils.DimenUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * @author simonw
 * @version 1.0
 * @created 2016/12/31
 */

public class XyjfActivity extends BaseActivity implements View.OnClickListener {
    private TextView tvXy, tvJf, tv0, tv1, tv2, tv3, btnYuyue;
    private LinearLayout layoutXyjf, layoutXyBar;
    private RecyclerView recycler;
    private XyjfAdapter mAdapter;
    private View vClose, vMask;
    private ImageView ivLcNew;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xyjf);

        initViews();

        GridLayoutManager mgr = new GridLayoutManager(this, 3);
        recycler.setLayoutManager(mgr);
        recycler.addItemDecoration(new XyjfActivity.SpacesItemDecoration(DimenUtils.dip2px(XyjfActivity.this, 0.5f),
                DimenUtils.dip2px(XyjfActivity.this, 0.5f)));
        recycler.setAdapter(mAdapter = new XyjfAdapter());
    }

    private void initViews() {
        btnYuyue = (TextView) findViewById(R.id.btn_yuyue);
        vClose = findViewById(R.id.v_close);
        vMask = findViewById(R.id.v_mask);
        ivLcNew = (ImageView) findViewById(R.id.iv_lc_new);
        layoutXyjf = (LinearLayout) findViewById(R.id.layout_xyjf);
        layoutXyBar = (LinearLayout) findViewById(R.id.layout_xy_bar);
        recycler = (RecyclerView) findViewById(R.id.recycler);
        tvXy = (TextView) findViewById(R.id.tv_xy);
        tvJf = (TextView) findViewById(R.id.tv_jf);
        tv0 = (TextView) findViewById(R.id.tv_0);
        tv1 = (TextView) findViewById(R.id.tv_1);
        tv2 = (TextView) findViewById(R.id.tv_2);
        tv3 = (TextView) findViewById(R.id.tv_3);

        btnYuyue.setOnClickListener(this);
        vClose.setOnClickListener(this);
        tvXy.setOnClickListener(this);
        tvJf.setOnClickListener(this);
        tv0.setOnClickListener(this);
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);


        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    class XyjfAdapter extends RecyclerView.Adapter<XyjfAdapter.XyjfHolder> {
        private List<AllClass.Class> data = App.parent1s;

        void swapData(List<AllClass.Class> newData) {
            data = newData;
            notifyDataSetChanged();
        }

        @Override
        public XyjfHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new XyjfHolder(LayoutInflater.from(XyjfActivity.this).inflate(R.layout.item_xx, null));
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void onBindViewHolder(XyjfHolder holder, int position) {
            Picasso.with(XyjfActivity.this).load(Server.SERVER + data.get(position).getImg()).into(holder.ivPic);
            holder.tvName.setText(data.get(position).getName());
            holder.tvPrice.setText("￥" + data.get(position).getPrice());
        }

        @Override
        public int getItemCount() {
            return data.size();
        }

        class XyjfHolder extends RecyclerView.ViewHolder {
            ImageView ivPic;
            TextView tvName;
            TextView tvPrice;

            XyjfHolder(View itemView) {
                super(itemView);
                ivPic = (ImageView) itemView.findViewById(R.id.iv_pic);
                tvName = (TextView) itemView.findViewById(R.id.tv_name);
                tvPrice = (TextView) itemView.findViewById(R.id.tv_price);
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        vMask.setVisibility(View.VISIBLE);
                        vClose.setVisibility(View.VISIBLE);
                        ivLcNew.setVisibility(View.VISIBLE);
                    }
                });
            }
        }
    }

    public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        private int leftRight;
        private int topBottom;

        public SpacesItemDecoration(int leftRight, int topBottom) {
            this.leftRight = leftRight;
            this.topBottom = topBottom;
        }

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDraw(c, parent, state);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            GridLayoutManager layoutManager = (GridLayoutManager) parent.getLayoutManager();
            //判断总的数量是否可以整除
            int totalCount = layoutManager.getItemCount();
            int surplusCount = totalCount % layoutManager.getSpanCount();
            int childPosition = parent.getChildAdapterPosition(view);
            if (layoutManager.getOrientation() == GridLayoutManager.VERTICAL) {//竖直方向的
                if (surplusCount == 0 && childPosition > totalCount - layoutManager.getSpanCount() - 1) {
                    //后面几项需要bottom
                    outRect.bottom = topBottom;
                } else if (surplusCount != 0 && childPosition > totalCount - surplusCount - 1) {
                    outRect.bottom = topBottom;
                }
                if ((childPosition + 1) % layoutManager.getSpanCount() == 0) {//被整除的需要右边
                    outRect.right = leftRight;
                }
                outRect.top = topBottom;
                outRect.left = leftRight;
            } else {
                if (surplusCount == 0 && childPosition > totalCount - layoutManager.getSpanCount() - 1) {
                    //后面几项需要右边
                    outRect.right = leftRight;
                } else if (surplusCount != 0 && childPosition > totalCount - surplusCount - 1) {
                    outRect.right = leftRight;
                }
                if ((childPosition + 1) % layoutManager.getSpanCount() == 0) {//被整除的需要下边
                    outRect.bottom = topBottom;
                }
                outRect.top = topBottom;
                outRect.left = leftRight;
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_xy:
                layoutXyjf.setBackgroundResource(R.drawable.xyjf_0);
                layoutXyBar.setVisibility(View.VISIBLE);
                tv0.performClick();
                break;
            case R.id.tv_jf:
                layoutXyjf.setBackgroundResource(R.drawable.xyjf_1);
                layoutXyBar.setVisibility(View.GONE);
                mAdapter.swapData(App.parent5s);
                break;
            case R.id.tv_0:
                tv0.setBackgroundResource(R.drawable.bg_rectangle_brown);
                tv1.setBackgroundResource(R.drawable.bg_rectangle);
                tv2.setBackgroundResource(R.drawable.bg_rectangle);
                tv3.setBackgroundResource(R.drawable.bg_rectangle);
                tv0.setTextColor(getResources().getColor(R.color.text_white));
                tv1.setTextColor(getResources().getColor(R.color.brown));
                tv2.setTextColor(getResources().getColor(R.color.brown));
                tv3.setTextColor(getResources().getColor(R.color.brown));
                mAdapter.swapData(App.parent1s);
                break;
            case R.id.tv_1:
                tv0.setBackgroundResource(R.drawable.bg_rectangle);
                tv1.setBackgroundResource(R.drawable.bg_rectangle_brown);
                tv2.setBackgroundResource(R.drawable.bg_rectangle);
                tv3.setBackgroundResource(R.drawable.bg_rectangle);
                tv0.setTextColor(getResources().getColor(R.color.brown));
                tv1.setTextColor(getResources().getColor(R.color.text_white));
                tv2.setTextColor(getResources().getColor(R.color.brown));
                tv3.setTextColor(getResources().getColor(R.color.brown));
                mAdapter.swapData(App.parent2s);
                break;
            case R.id.tv_2:
                tv0.setBackgroundResource(R.drawable.bg_rectangle);
                tv1.setBackgroundResource(R.drawable.bg_rectangle);
                tv2.setBackgroundResource(R.drawable.bg_rectangle_brown);
                tv3.setBackgroundResource(R.drawable.bg_rectangle);
                tv0.setTextColor(getResources().getColor(R.color.brown));
                tv1.setTextColor(getResources().getColor(R.color.brown));
                tv2.setTextColor(getResources().getColor(R.color.text_white));
                tv3.setTextColor(getResources().getColor(R.color.brown));
                mAdapter.swapData(App.parent3s);
                break;
            case R.id.tv_3:
                tv0.setBackgroundResource(R.drawable.bg_rectangle);
                tv1.setBackgroundResource(R.drawable.bg_rectangle);
                tv2.setBackgroundResource(R.drawable.bg_rectangle);
                tv3.setBackgroundResource(R.drawable.bg_rectangle_brown);
                tv0.setTextColor(getResources().getColor(R.color.brown));
                tv1.setTextColor(getResources().getColor(R.color.brown));
                tv2.setTextColor(getResources().getColor(R.color.brown));
                tv3.setTextColor(getResources().getColor(R.color.text_white));
                mAdapter.swapData(App.parent4s);
                break;
            case R.id.btn_yuyue:
                Intent intent = new Intent(XyjfActivity.this,OrderAddActivity.class);
                if(layoutXyBar.getVisibility() == View.VISIBLE){
                    intent.putExtra("class_data", "洗衣");
                }else{
                    intent.putExtra("class_data", "家纺");
                }
                startActivity(intent);
                break;
            case R.id.v_close:
                vMask.setVisibility(View.GONE);
                vClose.setVisibility(View.GONE);
                ivLcNew.setVisibility(View.GONE);
                break;
        }
    }
}
